<?php
require_once ('passwords.php');
function GetConnection()
{
	global $dbusername;
	global $dbpassword;	
	global $dbname;
	$conn = new mysqli('localhost', $dbusername, $dbpassword, $dbname);
	if($conn->error)
	{
		throw new Exception($conn->error, 1);
	}
	return $conn;
}
function EscapeRow($row, $conn)
{
	$arr = array();
	foreach ($row as $key => $value) {
		$arr[$key] = $conn->real_escape_string($value);
	}
	return $arr;	
}
	function curl_get($path, $method="GET", $data=null)
	{
		$curl = curl_init();
		curl_setopt($curl, CURLOPT_HEADER, FALSE);
		curl_setopt($curl, CURLOPT_TIMEOUT, 20);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); // no echo, just return result
		curl_setopt($curl, CURLOPT_USERAGENT, 'SUNY New Paltz web course');
		if ($method === 'POST') {
			curl_setopt($curl, CURLOPT_POST, TRUE);
			curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
		}elseif(!empty($data)){
			if(strpos($path, '?') === false)
				$path .= '?';
			else
				$path .= '&';
			$path .= http_build_query($data);
		}
		//echo $path;
		curl_setopt($curl, CURLOPT_URL, $path);

		return curl_exec($curl);
	}
